package com.macro.mall.portal.controller;

import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.model.MbrMemberCollect;
import com.macro.mall.model.MbrMemberHistory;
import com.macro.mall.portal.service.MbrMemberCollectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.List;

/**
* 会员收藏表(mbr_member_collect）
* @author gaodx
* @create 2020-08-11 15:43
**/
@Slf4j
@RestController
@RequestMapping("mbrMemberCollect")
@Api(tags = "MbrMemberCollectController", description = "会员收藏表(mbr_member_collect）")
public class MbrMemberCollectController {

    /** CurVideoService服务 */
    @Autowired
    private MbrMemberCollectService memberCollectService;

    @ApiOperation("用户收藏列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<MbrMemberCollect>> list(Principal principal, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                           @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        if(principal==null){
            return CommonResult.unauthorized(null);
        }
        List<MbrMemberCollect> mbrMemberCollects = memberCollectService.find(new MbrMemberCollect(), pageNum, pageSize);
        return CommonResult.success(CommonPage.restPage(mbrMemberCollects));
    }

    @ApiOperation("用户收藏数量")
    @RequestMapping(value = "/count", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<Long> count(Principal principal) {
        if(principal==null){
            return CommonResult.unauthorized(null);
        }
        long count = memberCollectService.count(new MbrMemberHistory());
        return CommonResult.success(count);
    }


    @ApiOperation("添加")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult save(Principal principal, @ApiParam(value = "id") @RequestParam(value = "id", required = true) Integer id) {
        try {
            if(principal==null){
                return CommonResult.unauthorized(null);
            }
            memberCollectService.saveNotNull(id);
            return CommonResult.success(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CommonResult.failed();

    }

    @ApiOperation("删除")
    @RequestMapping(value = "/delById", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult deleteById(@ApiParam(value = "id") @RequestParam(value = "id", required = true) String id) {
        try {
            memberCollectService.deleteById(id);
            return CommonResult.success(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CommonResult.failed();

    }




}